package cz.data.domain.elasticsearch.listener;

import cz.data.common.elastic.CacheClientBean;
import cz.data.common.rabbitmq.config.RabbitConstant;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Slf4j
@Component
public class RabbitMqListener {

    @ApiOperation(value = "数据源变更监听", notes = "刷新变更数据源缓存")
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(), //注意这里不要定义队列名称,系统会随机产生
            exchange = @Exchange(value = RabbitConstant.DSC_FANOUT_EXCHANGE, type = ExchangeTypes.FANOUT)
    ))
    public void datasourceChangeListener(Map<String, ?> map) {
        String sourceId = (String) map.get("data");
        if (StringUtils.isNotBlank(sourceId)) {
            log.info("监听到数据源变更事件,无效内存缓存");
            CacheClientBean.invalid(sourceId);
        }
    }
}
